package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IProductBannerDAO;
import org.jsola.hr.dto.ProductBannerAddDTO;
import org.jsola.hr.dto.ProductBannerUpdateDTO;
import org.jsola.hr.entity.ProductBannerDO;
import org.jsola.hr.query.ProductBannerQuery;
import org.jsola.hr.service.IProductBannerService;
import org.jsola.hr.vo.ProductBannerListVO;
import org.jsola.hr.vo.ProductBannerVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品banner
 *
 * @author zhr
 */
@Service("hrProductBannerServiceImpl")
public class ProductBannerServiceImpl implements IProductBannerService {

    @Autowired
    private IProductBannerDAO productBannerDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ProductBannerVO save(ProductBannerAddDTO productBannerAddDTO, TokenUser tokenUser) {
        List<ProductBannerDO> productBannerDOList = selectBySite(tokenUser.getSiteId());
        ParamException.isFalse(productBannerDOList.parallelStream().anyMatch(productBannerDO
                -> productBannerAddDTO.getProductId().equals(productBannerDO.getProductId())), "被选择的轮播产品不可重复");
        // 转DO
        ProductBannerDO productBannerDO = productBannerAddDTO.to(ProductBannerDO.class);
        // 保存
        productBannerDO = save(productBannerDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return productBannerDO.to(ProductBannerVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(ProductBannerUpdateDTO productBannerUpdateDTO, TokenUser tokenUser) {
        List<ProductBannerDO> productBannerDOList = selectBySite(tokenUser.getSiteId());
        productBannerDOList = productBannerDOList.parallelStream().filter(productBannerDO
                -> !productBannerUpdateDTO.getId().equals(productBannerDO.getId())).collect(Collectors.toList());
        ParamException.isFalse(productBannerDOList.parallelStream().anyMatch(productBannerDO
                -> productBannerUpdateDTO.getProductId().equals(productBannerDO.getProductId())), "被选择的轮播产品不可重复");
        //转DO
        ProductBannerDO productBannerDO = productBannerUpdateDTO.to(ProductBannerDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(productBannerDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... productBannerIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) productBannerIds);
    }


    @Override
    public ProductBannerVO selectById(Long productBannerId, String siteId) {
        ProductBannerDO productBannerDO = selectDOById(productBannerId, siteId);
        if (productBannerDO == null) {
            return null;
        }
        return productBannerDO.to(ProductBannerVO.class);
    }

    @Override
    public List<ProductBannerListVO> select(ProductBannerQuery productBannerQuery, String siteId) {
        List<ProductBannerDO> productBannerDOList = selectDO(productBannerQuery, siteId);
        if(CollectionUtils.isEmpty(productBannerDOList)) {
            return productBannerDOList == null ? null : new ArrayList<>();
        }
        return productBannerDOList.stream()
                .map(productBannerDO -> productBannerDO.to(ProductBannerListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ProductBannerQuery productBannerQuery, String siteId) {
        Example example = buildExample(productBannerQuery,siteId);
        return productBannerDAO.selectCountByExample(example);
    }

    @Override
    public Page<ProductBannerListVO> selectPage(ProductBannerQuery productBannerQuery, String siteId) {
        Example example = buildExample(productBannerQuery,siteId);
        Page<ProductBannerDO> page = productBannerDAO.selectPageByExample(example,
                productBannerQuery.getPageNo(),
                productBannerQuery.getPageSize());

        return page.to(ProductBannerListVO.class);
    }

    @Override
    public ProductBannerDO selectDOById(Long productBannerId, String siteId) {
        return listById(productBannerId, siteId);
    }

    @Override
    public List<ProductBannerDO> selectDO(ProductBannerQuery productBannerQuery, String siteId) {
        Example example = buildExample(productBannerQuery,siteId);
        return productBannerDAO.selectByExample(example);
    }

    @Override
    public List<ProductBannerDO> selectDoByProductIds(List<String> productIds, String siteId) {
        if (CollectionUtils.isEmpty(productIds)) {
            return new ArrayList<>();
        }
        Example example = new Example(ProductBannerDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId)
                .andIn("productId", productIds);
        return productBannerDAO.selectByExample(example);
    }

    @Override
    public List<ProductBannerDO> selectBySite(String siteId) {
        Example example = new Example(ProductBannerDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        example.orderBy("gmtCreate").desc();
        return productBannerDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param productBannerQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(ProductBannerQuery productBannerQuery, String siteId) {
        Example example = new Example(ProductBannerDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,productBannerQuery.getOrders());
        return example;
    }
}




